package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.DayPersonal;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthPersonal;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MonthPersonalStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DayMpMapper dayMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MonthPersonalMapper monthPersonalMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void monthPersonalStatistics(String oId) {

        int mId = timeUtil.getMonthTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            MonthPersonal monthPersonal = monthPersonalMapper.selectMonthPersonalByDIdAndCId(mId, menuOrderList.get(i).getCId());
            if(monthPersonal == null) {
                monthPersonal = new MonthPersonal();
                monthPersonal.setMpsId(monthPersonalMapper.selectCount(null) + 1);
                monthPersonal.setMpId(mId);
                monthPersonal.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthPersonal.setMpExpend(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                monthPersonal.setMpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                monthPersonalMapper.insert(monthPersonal);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthPersonal.setMpExpend(monthPersonal.getMpExpend() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                monthPersonal.setMpCalorie(monthPersonal.getMpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                monthPersonalMapper.updateById(monthPersonal);
            }
        }
    }

}
